|
|
|
|
|
|
|
On Error GoTo [Line ¦ Label] |
|
|
|
|
|
|
|
|
The first version of this statement will enable the error-handling routine that begins at the line or label specified in the statement. If an error occurs while this error routine is enabled, control will jump to the first line of the code in the error routine. This is the most common way of using the On Error statement. |
|
|
|
|
|
|
|
|
Before going on to the next version of the statement, investigate the use of line numbers or labels for the error routine. Before Windows programming was done in Visual Basic, most basic programs written for DOS computers were created with line numbers assigned by the basic editor to each line in the program code. This was both good and bad. First, the line numbers had to be set so that the programmer could add new lines in between those that were already there. The good part of line numbers was that when an error occurred, you could have the line number printed where the error occurred, enabling you to find the problem and fix it quicker. This was done using some properties of the Err object that existed even back in the DOS days. |
|
|
|
|
|
|
|
|
These days, using line numbers in Visual Basic has fallen into a programming black hole. Although they are still supported, most programmers don't use them. The Err object in Visual Basic that is discussed in the next section still supports the line number property. |
|
|
|
|
|
|
|
|
Getting back to the On Error statement, if the second version of the statement is used, control will jump to the statement immediately following the one where the error occurred. This enables you to trap an error and have the application simply ignore it. Of course, this only works if the line of code that has the error will not effect any of the following code due because of the error. |
|
|
|
|
|
|
|
|
The third and final version of the On Error statement will disable the error-handling routine in the current procedure. This version can be used to temporarily disable an error routine in a procedure. You would then need to execute the first or second version of the statement to re-enable the error routine. When you add an error routine to an event procedure or to a subroutine or function that you create, it should be placed at the bottom of the routine. In addition, you need to place an Exit Sub statement immediately preceding the error routine label. This will enable the routine to exit normally if execution flows to the bottom of the routine. |
|
|
|
|
|